<?php
/**
 * Pager
 *-------------------------
 *
 * base function static calss
 *
 * PHP versions 5
 *
 * LICENSE: This source file is from CouponMountain Ver3.
 * The copyrights is reserved by http://www.mezimedia.com.
 * Copyright (c) 2006, Mezimedia. All rights reserved.
 *
 * @author     James.Huang <james_huang@mezimedia.com>
 * @copyright  (C) 2004-2006 Mezimedia.com
 * @license    http://www.mezimedia.com  PHP License 5.0
 * @version    CVS: $Id: Pager.php,v 1.2 2008/01/30 06:24:31 kevin Exp $
 * @link       http://www.couponmountain.com/
 * @deprecated File deprecated in Release 3.0.0
 */


class Pager
{
	public $item;
	public $page;
	public $total;
	public $totalNum;
	public $totalPage;
	public $pattern;
	public $frNum;
	public $toNum;
	public $paging;
	public $prev;
	public $next;
	
	public $pagArr		= array();
	public $pagStr		= 'pag';
	
	public $dsPageOne	= false;	// display first page ?
	public $firstPage	= false;	// from which first page ?
	
	public function __construct($item, $page, $total, $pattern) {
		// initialize the attributes
		$this->__initialize($item, $page, $total, $pattern);
		// main process for pager
		$this->__process();
	}

	/**
	 * initialize the Pager
	 *
	 * @param int $item			;per Page Size
	 * @param int $page			;Current Page
	 * @param mix $totalNum		;Data Array or Total munber of items
	 * @param array $pattern	;Display pattern (include paging/prev/next field)
	 * @return 
	 */
	public function __initialize ($item = null, $page = null, $total = null, $pattern = null) {
		$this->item			= is_null($item) ? 5 : $item;
		$this->page			= (is_null($page) || $page <= 0) ? 1 : $page;
		$this->total		= $total;
		$this->totalNum		= is_array($total) ? count($total) : $total;
		$this->totalPage	= 0;
		$this->pattern		= ($pattern == null) ? array() : $pattern;
		$this->frNum		= 0;
		$this->toNum		= 0;
		$this->paging		= '';
		$this->prev			= 'Prev';
		$this->next			= 'Next';
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// get or set functions
	
	public function __process () {
		if (!$this->parsePager()) $this->__initialize();
	}

	public function getPrevStr () {
		return $this->prev;
	}

	public function setPrevStr ($str) {
		$this->prev = $str;
	}

	public function getNextStr () {
		return $this->next;
	}

	public function setNextStr ($str) {
		$this->next = $str;
	}

	public function getPageStr () {
		return $this->pagStr;
	}

	public function setPageStr ($str) {
		$this->pagStr = $str;
	}

	public function getFirstPage () {
		return $this->firstPage;
	}

	public function setFirstPage ($pageID) {
		$this->firstPage = $pageID;
	}

	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// main functions

	/**
	 * Convert Pager class attributes to a array.
	 *
	 * @return array
	 */
	public function toArray () {
		return array(
			'totalNum'	=> $this->totalNum,
			'totalPage'	=> $this->totalPage,
			'itemNum'	=> $this->item,			
			'frNum'		=> $this->frNum,
			'toNum'		=> $this->toNum,
			'pageArr'	=> $this->pagArr,
			'pageStr'	=> $this->paging,
			'prevStr'	=> $this->prev,
			'nextStr'	=> $this->next,
		);
	}

	/**
	 * Cooperate with data class to fetch items for each page.
	 * Use sql LIMIT to separate page items.
	 *
	 * @return boolean
	 */
	public function parsePager () {
		if ($this->totalNum > 0) {
			// for paging field
			$this->totalPage = ceil($this->totalNum / $this->item);
			$this->page = ($this->page > $this->totalPage) ? $this->totalPage : $this->page;
			$this->frNum = $this->item * ($this->page - 1);
			$this->toNum = ($this->page == $this->totalPage) ? $this->totalNum : $this->item * $this->page;
			for ($i = 1; $i <= $this->totalPage; $i++) {
				if ($this->page == $i) {
					$pages[$i] = '<b>'.$i.'</b>';
				} else if ($this->page == $i && $this->totalPage == $i) {
					$pages[$i] = $this->totalPage;
				} else {
					$pages[$i] = $this->makeURL($i);
				}
			}
//			fBase::printArray($pages);
			if (isset($this->pattern['Mode'])) {
				switch ($this->pattern['Mode']) {
					// Google Style Paging
					case '1' : 
						$start_pos = ($this->page - 10 < 0) ? 0 : ($this->page - 10);
						$end_pos = ($this->page + 9 > $this->totalPage) ? $this->totalPage : ($this->page + 9);
						$pages = fBase::array_chop($pages, $start_pos, $end_pos);
						break;
					// My Style Paging
					case '2' : 
						// don't split pages array when total page < 10
						if ($this->totalPage > 10) {
							$start_pos = ($this->page - 5 < 0) ? 0 : ($this->page - 5);
							$start_pos = ($start_pos + 10 > $this->totalPage) ? ($this->totalPage - 10) : $start_pos;
							$pages = array_slice($pages, $start_pos, 10);
						}
						break;
					// Show All Paging
					default : break;	
				}
			}
			$this->paging = implode(" ", $pages);
			// get page array
			if (is_array($this->total)) {
				for ($i = $this->frNum; $i < $this->toNum; $i++) {
					array_push($this->pagArr, $this->total[$i]);
				}
			}
			// for prev/next field
			$prevStr = (array_key_exists('Prev', $this->pattern)) ? $this->pattern['Prev'] : $this->prev;
			$nextStr = (array_key_exists('Next', $this->pattern)) ? $this->pattern['Next'] : $this->next;
			if ($this->page != 1) {
				$page = $this->page - 1;
				$this->prev = $this->makeURL($page, $prevStr);
			} else {
				$this->prev = $prevStr;
			}
			if ($this->page != $this->totalPage) {
				$page = $this->page + 1;
				$this->next = $this->makeURL($page, $nextStr);
			} else {
				$this->next = $nextStr;
			}
			return true;
		}
		return false;
	}

	/**
	 * Convert Pager class attributes to a array.
	 *
	 * @param int $pageNo
	 * @param int $keyword	;Href Content Field
	 *
	 * @return array
	 */
	public function makeURL ($pageNo = 1, $keyword = null) {
		// get first page
		$frPagNum = ($this->firstPage) ? $this->firstPage : 1;
		if ($pageNo < $frPagNum) return ;
		// default page url.
		$pageUrl = $_SERVER['REQUEST_URI']."?".$this->pagStr."=".$pageNo;
		// make paging field
		if (array_key_exists('Paging', $this->pattern)) {
			if (preg_match("/{page}/i", $this->pattern['Paging'])) {
				$pageUrl = str_replace("{page}", $pageNo, $this->pattern['Paging']);
			}
		}
		$pageKey = ($keyword === null) ? $pageNo : $keyword;
		$hrefStr = "<a href=\"$pageUrl\">$pageKey</a>";
		return $hrefStr;
	}

}
?>